﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace USBAutoPlayer
{
    using System;
    using System.IO;
    using System.Management;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        const int WM_DEVICECHANGE = 0x219; //u盘如后的硬件设备改变消息
        const int DBT_DEVICEARRIVAL = 0x8000; //u盘可用
        const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //设备删除

        string SN = "4C530102100829110303";
        string currentSN;
        string Path1 = @".\Data\video.mp4";

        [DllImport("user32", EntryPoint = "ShowCursor")]
        public extern static bool ShowCursor(bool show);

        [DllImport("user32.dll")]
        static extern void BlockInput(bool Block);


        public Form1()
        {
            InitializeComponent();            
        }

        private void PlayVideo() //播放视频
        {
            ShowCursor(false);
            BlockInput(true);
            axWindowsMediaPlayer1.URL = Path1;
            axWindowsMediaPlayer1.settings.setMode("loop", true);
            axWindowsMediaPlayer1.uiMode = "none";
            axWindowsMediaPlayer1.enableContextMenu = false;
            axWindowsMediaPlayer1.Ctlenabled = false;
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }


        private void T1(object info) //读取设备
        {
            ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = cimobject.GetInstances(); //获取设备集合
            foreach (var mo in moc)
            {
                if (mo.Properties["InterfaceType"].Value.ToString() == "USB")
                {
                    try
                    {
                        string[] devInfos = mo.Properties["PNPDeviceID"].Value.ToString().Split('&'); //即插即用设备信息
                        string serialNumber = devInfos[3].Split('\\')[1];
                        lock (this)
                        {
                            if (serialNumber.Trim() == SN.Trim() && serialNumber != currentSN)
                            {
                                currentSN = serialNumber;
                                // MessageBox.Show("设备插入：" + serialNumber);
                                PlayVideo();
                            }
                        }                       
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
            }            
        }


        protected override void WndProc(ref Message m) //处理Win系统消息
        {

            try
            {
                if (m.Msg == WM_DEVICECHANGE)
                {
                    if (m.WParam.ToInt32() == DBT_DEVICEARRIVAL)
                    {
                        DriveInfo[] info = DriveInfo.GetDrives();
                        foreach (var item in info)
                        {
                            if (item.DriveType == DriveType.Removable) //是移动设备
                            {
                                Thread t = new Thread(new ParameterizedThreadStart(T1));
                                t.Start(item);
                            }
                        }
                    }

                    if (m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
                    {
                        currentSN = "";
                        Stop();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            base.WndProc(ref m);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // PlayVideo();
            this.WindowState = FormWindowState.Minimized;
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.enableContextMenu = false;
        }

        private void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            TopMost = true;
            if ((int)axWindowsMediaPlayer1.playState == 1)
            {
                // System.Threading.Thread.Sleep(1000);
                // axWindowsMediaPlayer1.Ctlcontrols.play();
                this.WindowState = FormWindowState.Minimized;
            }
            else if ((int)axWindowsMediaPlayer1.playState == 3)
            {
                axWindowsMediaPlayer1.fullScreen = true;
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e) //退出播放
        {
            if (e.KeyCode == Keys.Escape)
            {
                Stop();
            }
        }

        private void Stop()
        {
            ShowCursor(true);
            BlockInput(false);
            axWindowsMediaPlayer1.Ctlenabled = true;
            TopMost = false;
            axWindowsMediaPlayer1.fullScreen = false;
            axWindowsMediaPlayer1.Ctlcontrols.stop();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                notifyIcon1.Visible = true;
            }
            else
            {
                this.Show();
                notifyIcon1.Visible = false;
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            notifyIcon1.Visible = false;
            this.WindowState = FormWindowState.Normal;
        }
    }
}
